<?php
namespace Crm\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;

class MaterialCategoryTable extends AbstractTable
{

    protected $table = 'hy_material_category';

    protected $id = 'material_category_id';

    public function find($query = array())
    {
        // 查询条件 城市 商户类型
        $select = new Select(array(
            'o' => $this->getTable()
        ));
        $select->columns(array(
            'material_category_id',
            'material_catalog_id',
            'material_category_name'
        ));
        $select->join(array(
            'c' => 'hy_material_catalog'
        ), 'c.material_catalog_id=o.material_catalog_id', array(
            'material_catalog_name'
        ), $select::JOIN_LEFT);
        // if($query['catalog_id']) {
        // $select->where(array('c.material_catalog_id' => $query['catalog_id']));
        // }
        
        if (! empty($query['material_category_name'])) {
            $spec = function (Where $where) use($query) {
                $where->like('o.material_category_name', '%'.$query['material_category_name'] . "%");
            };
            
            $select->where($spec);
        }
        
        $select->order('o.material_category_id DESC');
        // $select->limit($limit);
        
        return $this->fetchPaginate($select, $query);
    }

    public function findAll()
    {
        $rowset = $this->select();
        $result = $rowset->toArray();
        return $result;
    }

    public function findOne($id)
    {
        $rowset = $this->select(array(
            $this->getId() => $id
        ));
        $row = $rowset->current();
        return $row;
    }

    public function findById()
    {}

    public function create($data)
    {
        return $this->insert($data);
    }

    public function updateById($data, $id)
    {
        return $this->update($data, array(
            $this->getId() => $id
        ));
    }

    public function remove()
    {}

    public function updateItem($id, $data)
    {
        if (empty($id)) {
            throw new \Exception("id field is invalid!");
        }
        $data = array(
            'resource_category_name' => $data['resource_category_name']
        );
        return $this->update($data, array(
            'material_catalog_id' => $id
        ));
    }

    public function deleteItem($id)
    {
        if (empty($id)) {
            throw new Exception("id field is invalid!");
        }
        return $this->delete(array(
            'material_catalog_id' => $id
        ));
    }

    public function addItem($data)
    {
        $data = array(
            'resource_category_name' => $data['resource_category_name']
        );
        
        $this->insert($data);
        $id = $this->lastInsertValue;
        
        return $id;
    }

    public function getItem($id)
    {
        if (empty($id)) {
            throw new Exception("id field is invalid!");
        }
        $sql = 'SELECT * FROM `hy_material_category` WHERE `material_catalog_id` =:material_catalog_id';
        $statement = $this->getAdapter()->query($sql);
        $results = $statement->execute(array(
            ':material_catalog_id' => $id
        ));
        $row = $results->current();
        return $row;
    }
}